All files / src/app/api/mock-dsp/[participant]/catalog/request route.ts

0% Statements 0/16
0% Branches 0/6
0% Functions 0/4
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215                                                                                                                                                                                                                                                                                                                                                                                                                                             
import { NextResponse } from "next/server";
 
export const dynamic = "force-dynamic";
 
/**
 * Mock DSP 2025-1 `/catalog/request` endpoint.
 *
 * Phase 26b catalog-crawler hits this URL once every 5 minutes for each
 * participant whose `dspCatalogUrl` is pointed here. The response is a
 * realistic `dcat:Catalog` JSON-LD doc so the enricher can materialise
 * federated `(:HealthDataset {source:"federated"})` nodes and the
 * Phase 26d NLQ templates can actually return something.
 *
 * Stand-in for real per-participant DSP connectors — avoids having to
 * run five separate mock servers or beg AlphaKlinik for HTTPS infra.
 *
 * POST body is the crawler's empty QuerySpec; ignored. The `participant`
 * path param drives the catalog payload via MOCK_CATALOGS below.
 */
 
type Coding = { system: string; code: string; display: string };
interface MockDataset {
  id: string;
  title: string;
  description: string;
  theme: Coding;
  license: string;
  requiresCredential?: string;
}
interface MockPublisher {
  did: string;
  name: string;
  country: string;
  datasets: MockDataset[];
}
 
const SNOMED = "http://snomed.info/sct";
const LICENSE_CC_BY = "https://creativecommons.org/licenses/by/4.0/";
 
const MOCK_CATALOGS: Record<string, MockPublisher> = {
  "alpha-klinik": {
    did: "did:web:alpha-klinik.de:participant",
    name: "AlphaKlinik Berlin",
    country: "DE",
    datasets: [
      {
        id: "dataset:alpha:t2dm-registry-2026",
        title: "Type 2 Diabetes Registry — Berlin 2026",
        description:
          "Longitudinal registry of 2,400 T2DM patients with HbA1c, BMI, treatment arm, and 5-year follow-up. OMOP-mapped.",
        theme: {
          system: SNOMED,
          code: "44054006",
          display: "Diabetes mellitus type 2",
        },
        license: LICENSE_CC_BY,
        requiresCredential: "DataQualityLabelCredential",
      },
      {
        id: "dataset:alpha:cv-icu-2025",
        title: "Cardiovascular ICU Outcomes — AlphaKlinik 2025",
        description:
          "De-identified ICU admissions with primary cardiovascular diagnoses, 30-day mortality, and length-of-stay.",
        theme: {
          system: SNOMED,
          code: "49601007",
          display: "Disorder of cardiovascular system",
        },
        license: LICENSE_CC_BY,
      },
    ],
  },
  "limburg-medical": {
    did: "did:web:lmc.nl:clinic",
    name: "Limburg Medical Centre",
    country: "NL",
    datasets: [
      {
        id: "dataset:lmc:t2dm-crossborder-2026",
        title: "Cross-border T2DM Cohort — Limburg 2026",
        description:
          "Type 2 Diabetes patients managed across NL/DE/BE border region with shared care coordination records.",
        theme: {
          system: SNOMED,
          code: "44054006",
          display: "Diabetes mellitus type 2",
        },
        license: LICENSE_CC_BY,
        requiresCredential: "DataQualityLabelCredential",
      },
    ],
  },
  "pharmaco-research": {
    did: "did:web:pharmaco.de:research",
    name: "PharmaCo Research AG",
    country: "DE",
    datasets: [
      {
        id: "dataset:pharmaco:oncology-phase2-2025",
        title: "Oncology Phase II Trial — Immunotherapy Cohort",
        description:
          "Phase II clinical trial data for PD-L1 inhibitor, 180 participants, 18-month follow-up, RECIST 1.1 endpoints.",
        theme: {
          system: SNOMED,
          code: "363346000",
          display: "Malignant neoplastic disease",
        },
        license: LICENSE_CC_BY,
        requiresCredential: "DataQualityLabelCredential",
      },
    ],
  },
  "institut-recherche-sante": {
    did: "did:web:irs.fr:hdab",
    name: "Institut de Recherche Santé",
    country: "FR",
    datasets: [
      {
        id: "dataset:irs:rare-diseases-fr-2026",
        title: "French National Rare Disease Registry Extract",
        description:
          "Pseudonymised cohort of 4,200 rare-disease patients with ICD-10 + Orphanet codes and longitudinal encounters.",
        theme: { system: SNOMED, code: "49649001", display: "Rare disease" },
        license: LICENSE_CC_BY,
      },
    ],
  },
};
 
function buildCatalog(pub: MockPublisher) {
  return {
    "@context": {
      dcat: "http://www.w3.org/ns/dcat#",
      dct: "http://purl.org/dc/terms/",
      odrl: "http://www.w3.org/ns/odrl/2/",
    },
    "@type": "dcat:Catalog",
    "dct:title": `${pub.name} — DSP Catalog`,
    "dct:publisher": {
      "@id": pub.did,
      "foaf:name": pub.name,
      "dct:spatial": pub.country,
    },
    "dcat:dataset": pub.datasets.map((ds) => ({
      "@id": ds.id,
      "@type": "dcat:Dataset",
      "dct:title": ds.title,
      "dct:description": ds.description,
      "dct:license": ds.license,
      "dct:spatial": pub.country,
      "dcat:theme": [
        {
          system: ds.theme.system,
          code: ds.theme.code,
          display: ds.theme.display,
        },
      ],
      "odrl:hasPolicy": ds.requiresCredential
        ? {
            "@id": `policy:${ds.id}`,
            "@type": "odrl:Set",
            "odrl:permission": [
              {
                "odrl:action": "use",
                "odrl:constraint": [
                  {
                    "odrl:leftOperand": "holdsCredential",
                    "odrl:operator": "eq",
                    "odrl:rightOperand": ds.requiresCredential,
                  },
                ],
              },
            ],
            requiresCredential: ds.requiresCredential,
          }
        : null,
    })),
  };
}
 
export async function POST(
  _request: Request,
  { params }: { params: Promise<{ participant: string }> },
): Promise<NextResponse> {
  const { participant } = await params;
  const pub = MOCK_CATALOGS[participant];
  if (!pub) {
    return NextResponse.json(
      { error: `No mock catalog for '${participant}'` },
      { status: 404 },
    );
  }
  return NextResponse.json(buildCatalog(pub));
}
 
// GET mirror for manual browser inspection. Real DSP 2025-1 uses POST, but
// a GET makes debugging from curl trivial without remembering the empty body.
export async function GET(
  _request: Request,
  { params }: { params: Promise<{ participant: string }> },
): Promise<NextResponse> {
  const { participant } = await params;
  const pub = MOCK_CATALOGS[participant];
  if (!pub) {
    return NextResponse.json(
      {
        error: `No mock catalog for '${participant}'`,
        available: Object.keys(MOCK_CATALOGS),
      },
      { status: 404 },
    );
  }
  return NextResponse.json(buildCatalog(pub));
}